<?php

class Core_View {
	
	public $path = '';

	protected static $_global_data = array();
	protected $_local_data         = array();

	public function __construct($path, $data = array()) {
		if (!file_exists($path))
			throw new Exception("View does not exist");

		$this->path        = $path;
		$this->_local_data = $data;
	}

	public function __toString() {
		return $this->render();
	}

	public function render() {
		extract(array_merge(View::$_global_data, $this->_local_data));

		ob_start();
		include $this->path;
		$buffer = ob_get_contents();
		ob_clean();

		return $buffer;
	}

	public function setLocal($index, $value) {
		$this->_local_data[$index] = $value;
	}

	public function bindLocal($index, &$value) {
		$this->_local_data[$index] = $value;
	}

	public static function setGlobal($index, $value) {
		View::$_global_data[$index] = $value;
	}

	public static function bindGlobal($index, &$value) {
		View::$_global_data[$index] = $value;
	}

}